package cn.hl.hm.service.impl;

import cn.hl.hm.dao.DriverMapper;
import cn.hl.hm.dao.DriverStatusHistoryMapper;
import cn.hl.hm.entity.Driver;
import cn.hl.hm.entity.DriverStatusHistory;
import cn.hl.hm.service.DriverStatusHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DriverStatusHistoryServiceImpl implements DriverStatusHistoryService {
    @Autowired
    private DriverStatusHistoryMapper driverStatusHistoryMapper;
    @Autowired
    private DriverMapper driverMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return driverStatusHistoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(DriverStatusHistory record) {
        return driverStatusHistoryMapper.insert(record);
    }

    @Override
    public int insertSelective(DriverStatusHistory record) {
        return driverStatusHistoryMapper.insertSelective(record);
    }

    @Override
    public DriverStatusHistory selectByPrimaryKey(Integer id) {
        return driverStatusHistoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(DriverStatusHistory record) {
        return driverStatusHistoryMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(DriverStatusHistory record) {
        return driverStatusHistoryMapper.updateByPrimaryKey(record);
    }

    @Override
    public int save(Integer driverId,String updateBy,String driverBeforeStatus,String driverAfterStatus){
        try {
            DriverStatusHistory record=new DriverStatusHistory();
            record.setCreateTime(new Date());
            record.setDriverId(driverId);
            record.setUpdateBy(Integer.valueOf(updateBy));
            record.setDriverBeforeStatus(Integer.valueOf(driverBeforeStatus));
            record.setDriverAfterStatus(Integer.valueOf(driverAfterStatus));
            return insertSelective(record);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int save(Integer driverId,String updateBy,String driverAfterStatus){
        try {
            Driver driver = driverMapper.selectByPrimaryKey(driverId);
            if (driver!=null) {
                DriverStatusHistory record=new DriverStatusHistory();
                record.setCreateTime(new Date());
                record.setDriverId(driverId);
                record.setUpdateBy(Integer.valueOf(updateBy));
                record.setDriverBeforeStatus(Integer.valueOf(driver.getDriverStatus()));
                record.setDriverAfterStatus(Integer.valueOf(driverAfterStatus));
                return insertSelective(record);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Integer> selectUpdateBys() {
        return driverStatusHistoryMapper.selectUpdateBys();
    }

    @Override
    public List<DriverStatusHistory> selectListByPrimaryKeySelective(Integer driverId, String startTime, String endTime, Integer updateBy) {
        return driverStatusHistoryMapper.selectListByPrimaryKeySelective(driverId, startTime, endTime, updateBy);
    }
}